const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
let users = new Map();
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.set("views", __dirname + "/views");
app.set('view engine', 'ejs');
// app.engine('html',ejs.__express);
// app.set('view engine','html');
app.get('/', function (req, res, next) {
    var ns = fs.readFile('./public/商品index.html', function (err, data) {
        if (err) {
            console.error(err);
            return;
        }
        res.end(data);

    });
});
app.post('/注册.html', function (req, res, next) {
    if (users.has(req.body.name)) {
        res.end('{"code":"4","msg":"用户已存在。"}');
    } else {
        users.set(req.body.name, {
            sessionid: 'B1E7C4C2009E322321374B5AEB03E383',
            个人信息: req.body
        });
        res.writeHead({
            'Content-Type': 'text/json:charset=UTF-8',
            'Set-Cookie': 'sessionid=' + users.get(req.body.name).sessionid
        });
        res.end('{"code":"2","msg":"注册成功。"}');
    }
})
app.post('/登录.html', function (req, res, next) {
    if (users.has(req.body.name) && users.get(req.body.name).个人信息.pwdMD5 === req.body.pwdMD5) {
        res.writeHead(200, {
            'Content-Type': 'text/json:charset=UTF-8',
            'Set-Cookie': 'sessionid=B1E7C4C2009E322321374B5AEB03E383'
        });
        users.get(req.body.name).sessionid = 'B1E7C4C2009E322321374B5AEB03E383';
        res.end(JSON.stringify({
            "code": 2,
            "msg": users.get(req.body.name)
        }))}
    })
app.listen(8080, () => {
    console.log(`App listening at port 8080`)
})
